// pages/edit4/edit4.js
Page({

  data: {  url1:'',url2:'',url3:"",url4:'',near_id:'',goods_id:'',last:""

  },
  getimage1(){
    wx.chooseImage({
      count:1,
      complete: (res) => {
        console.log(res);
        let file=res.tempFiles[0].path
        this.setData({
          url1:file
        })
        
        
      },
    })

  },
  getimage2(){
    wx.chooseImage({
      count:1,
      complete: (res) => {
        console.log(res);
        let file=res.tempFiles[0].path
        this.setData({
          url2:file
        })
        
        
      },
    })

  },
  getimage3(){
    wx.chooseImage({
      count:1,
      complete: (res) => {
        console.log(res);
        let file=res.tempFiles[0].path
        this.setData({
          url3:file
        })
        
        
      },
    })

  },
  getimage4(){
    wx.chooseImage({
      count:1,
      complete: (res) => {
        console.log(res);
        let file=res.tempFiles[0].path
        this.setData({
          url4:file
        })
        
        
      },
    })

  },
  del1(){
    this.setData({
      url1:''
    })
   },
   del2(){
    this.setData({
      url2:''
    })
   },
   del3(){
    this.setData({
      url3:''
    })
   },
   del4(){
    this.setData({
      url4:''
    })
   },
   navcanvas1(e){
    wx.navigateTo({
      url: '/pages/cropper/canvas?url='+e.currentTarget.dataset.url+"&flag=6",
    })
},
navcanvas2(e){
  wx.navigateTo({
    url: '/pages/cropper/canvas?url='+e.currentTarget.dataset.url+"&flag=7",
  })
},
navcanvas3(e){
  wx.navigateTo({
    url: '/pages/cropper/canvas?url='+e.currentTarget.dataset.url+"&flag=8",
  })
},
navcanvas4(e){
  wx.navigateTo({
    url: '/pages/cropper/canvas?url='+e.currentTarget.dataset.url+"&flag=9",
  })
},
submit(){
  if(this.data.url1==''||this.data.url2==""||this.data.url3==""||this.data.url4==""){
  return  wx.showToast({
      title: '请上传完整',
      icon:'none'
    })
  }
  wx.showLoading({
    title: '正在上传...',
    icon:'none'
  })
  wx.uploadFile({
    filePath: this.data.url1,
    name: 'file',
    url: 'https://zujieba.com/Api/upload/upload.html',
    success:(res1)=>{
      wx.uploadFile({
        filePath: this.data.url2,
        name: 'file',
        url: 'https://zujieba.com/Api/upload/upload.html',
        success:(res2)=>{
          wx.uploadFile({
            filePath: this.data.url3,
            name: 'file',
            url: 'https://zujieba.com/Api/upload/upload.html',
            success:(res3)=>{
              wx.uploadFile({
                filePath: this.data.url4,
                name: 'file',
                url: 'https://zujieba.com/Api/upload/upload.html',
                success:(res4)=>{
                  let data1=JSON.parse(res1.data).name
                  let data2=JSON.parse(res2.data).name
                  let data3=JSON.parse(res3.data).name
                  let data4=JSON.parse(res4.data).name
                  wx.request({
                    url: 'https://zujieba.com/Api/upload/canvas_four',
                    data:{pic_1:data1,img_1:data2,pic_2:data3,img_2:data4},
                    method:'POST',
                    success:(result)=>{
                      if(result.data.success==1){
                        wx.hideLoading()
                        console.log(result);
                        let url="https://zujieba.com/uploads/"+ result.data.data
                        console.log(url);
                        
                       this.throttle(()=>{ wx.navigateTo({
                         url: '/pages/fileprint/print/print?url='+url+'&near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+'&name='+result.data.data+'&time='+
                         result.data.addtime
                       })},2000)
                      }else{
                        wx.hideLoading()
                        wx.showToast({
                          title: result.data.tips,
                          icon:'none'
                        })
                      }
                    
                      
                    }
                  })
                }
              })
            }
          })
        }
      })
    }
  })
 
},
onLoad(options){
  this.setData({
    near_id:options.near_id,
    goods_id:options.goods_id
  })
},
 //节流
 throttle:function(func,wait){
  console.log(func,wait);
  
  let last =this.data.last
  console.log(last);
  
  let current_time= +new Date()
  if(current_time-last>wait){
    console.log(1111);
      console.log(current_time,current_time-last);
      
      func()
      last=+new Date()
      this.setData({
        last
      })
  }}
})